﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using PeekService;

namespace PeekWebApp
{
    public partial class active : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                string uname = Request.QueryString["user"];
                string checkpwd = Request.QueryString["id"];
                UserRegistry reg = new UserRegistryService().GetAll().Find(o => o.UserName ==uname);
                if (reg == null)
                {
                    CommonService.ShowAlert(Page, "该链接无效！");
                    Response.Redirect("http://" + Request.Url.Host + ":" + Request.Url.Port);
                }
                if (reg.CreateTime.Value.AddHours(24) < DateTime.Now||reg.IsEmailVerification)
                {
                    CommonService.ShowAlert(Page, "该链接已失效！");
                }
                else
                {
                    if (reg.ConfirmPassWord.Equals(DecryptService.Decrypt(checkpwd.Replace(" ","+"))))
                    {
                        reg.IsEmailVerification = true;
                        reg.State = 1;
                        reg.UserName = DecryptService.Encrypt(reg.UserName);
                        reg.PassWord = DecryptService.Encrypt(reg.PassWord);
                        reg.ConfirmPassWord = DecryptService.Encrypt(reg.ConfirmPassWord);
                        reg.Email = DecryptService.Encrypt(reg.Email);
                        new UserRegistryService().Update(reg);
                        CommonService.ShowAlert(Page, "激活成功！");
                        Response.Redirect("/");
                       
                    }
                    else
                    {
                        CommonService.ShowAlert(Page, "该链接无效");
                    }
                }
            }
            catch
            {
                CommonService.ShowAlert(Page, "激活失败，请联系管理员！");
            
            }
        }
    }
}